<template>
  <div class="app-container">
    <el-card>
      <el-tabs>
        <el-tab-pane label="账户设置">
          <el-form ref="userForm" label-width="200px" style="margin-top:20px" :rules="rules" :model="formData">
            <el-form-item label="姓名" prop="username">
              <el-input v-model="formData.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="formData.password" type="password" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="btnOk">更新</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="用户详情">
          <UserInfo />
        </el-tab-pane>
        <el-tab-pane label="岗位信息">
          <JobInfo />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
// 获取用户信息
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
import UserInfo from './components/user-info.vue'
import JobInfo from './components/job-info.vue'
export default {
  components: { UserInfo, JobInfo },
  data() {
    return {
      formData: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '该项不能为空', trigger: 'blur' }, {
          max: 10, message: '不能超过10个字符', trigger: 'blur'
        }],
        password: [{ required: true, message: '该项不能为空', trigger: 'blur' }, {
          max: 10, message: '不能超过10个字符', trigger: 'blur'
        }]
      },
      userId: this.$route.params.id
    }
  },
  async created() {
    // 密码返回的数据是经过加密的,所以formData的密码用空覆盖
    this.formData = { ...await getUserDetailById(this.userId), password: '' }
  },
  methods: {
    async btnOk() {
      // 校验规则
      this.$refs.userForm.validate()
      // 发送请求
      await saveUserDetailById(this.formData)
      //  提示用户
      this.$message.success('更新成功')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-input{
    width: 40%;
}
</style>
